﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.HardWare;

namespace WHUIdemo.Forms
{
    public partial class LED8TestFrm : Form
    {
        public LED8TestFrm()
        {
            InitializeComponent();
        }
        LED8Display led = new LED8Display();
        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
     
            serialPort1.Write(led.GetSendNum(textBox1.Text.Trim()));
            bool[] startLight=new bool[4];
            startLight[0]= whCheckBox1.Checked==true ? true : false;

            startLight[1]= whCheckBox2.Checked==true ? true : false;
            startLight[2] = whCheckBox3.Checked == true ? true : false;
            startLight[3] = whCheckBox4.Checked == true ? true : false;
                string stat = led.GetNeedStateFlgLight(startLight[0], startLight[1], startLight[2], startLight[3]);
                serialPort1.Write(stat);

            }
            else
            {
                MessageBox.Show("串口为打开");
            }
     
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();


                }
                else
                {
                    serialPort1.PortName = comboBox1.SelectedItem.ToString();

                    serialPort1.BaudRate = 2400;

                    serialPort1.DataBits = 8;

                    serialPort1.Parity = System.IO.Ports.Parity.None;
                    serialPort1.Open();

                    label4.Text = label4.Text + serialPort1.PortName + "已经打开";
                }


            }
            catch
            {

                MessageBox.Show("串口错误");
            }


        }

        private void LED8TestFrm_Load(object sender, EventArgs e)
        {
            string[] ArryPort = SerialPort.GetPortNames();
            comboBox1.Items.Clear();
            for (int i = 0; i < ArryPort.Length; i++)
            {
                comboBox1.Items.Add(ArryPort[i]);
            }
            if (ArryPort.Length>0)
            {
                comboBox1.SelectedIndex=0;
            }
        }

        private void LED8TestFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
             serialPort1.Close();
            }
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Write(led.GetClearAllStr());
            }
        }
    }
    }
